package org.qianye.soft.repository;

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.qianye.soft.entity.Clazz;
import org.qianye.soft.util.SpringApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class ClazzRepository {

	private static final Logger log = LoggerFactory.getLogger(ClazzRepository.class);

	private SessionFactory sessionFactory;

	@Resource(name = "sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	public void save(Clazz transientInstance) {
		log.debug("saving Clazz instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Clazz persistentInstance) {
		log.debug("deleting Clazz instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Clazz findById(java.lang.Integer id) {
		log.debug("getting Clazz instance with id: " + id);
		try {
			return (Clazz) getCurrentSession().get(Clazz.class, id);
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Clazz instance) {
		log.debug("finding Clazz instance by example");
		try {
			List results = getCurrentSession().createCriteria(Clazz.class).add(create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	List findByProperty(String propertyName, Object value) {
		log.debug("finding Clazz instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = String.format("from org.qianye.soft.entity.Clazz as model where model.%s= ?", propertyName);
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property header failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Clazz instances");
		try {
			String queryString = "from org.qianye.soft.entity.Clazz";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Clazz merge(Clazz detachedInstance) {
		log.debug("merging Clazz instance");
		try {
			Clazz result = (Clazz) getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void update(Clazz instance) {
		log.debug("attaching dirty Clazz instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ClazzRepository getInstance() {
		return SpringApplicationContext.getApplicationContext().getBean(ClazzRepository.class);
	}
}
